package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    /**
     * @author gengjian
     * @date 2024/12/11 14:45:22
     * 不仅要删房间信息，还要删房间配套关系、属性值关系、标签关系、房间租期关系、房间支付关系、房间图片
     */
    @Override
    public void customRemove(Long id) {
        //根据公寓id删除房间信息
        super.removeById(id);
        //根据公寓id删除房间配套关系
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
        //根据公寓id删除房间标签关系
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);
        //根据公寓id删除房间租期关系
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        //根据公寓id删除房间支付关系
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
        //根据公寓id删除房间图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM)
                        .eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        //根据公寓id删除房间属性值关系
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
    }

    @Override
    public void customSaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        //根据是否存在房间id判断是更新还是保存
        boolean b = roomSubmitVo.getId() != null;
        //最终都要保存或房间信息 多态
        super.saveOrUpdate(roomSubmitVo);
        //更新前要删除 因为一对多关系不好修改
        if (b){
            //根据公寓id删除房间配套关系
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
            //根据公寓id删除房间标签关系
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,roomSubmitVo.getId());
            roomLabelService.remove(roomLabelLambdaQueryWrapper);
            //根据公寓id删除房间租期关系
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
            //根据公寓id删除房间支付关系
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
            //根据公寓id删除房间图片
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM)
                    .eq(GraphInfo::getItemId,roomSubmitVo.getId());
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            //根据公寓id删除房间属性值关系
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
        }
        //保存房间图片信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = new ArrayList<>();
        for (GraphVo graphVo : graphVoList) {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setItemType(ItemType.ROOM);
            graphInfo.setItemId(roomSubmitVo.getId());
            graphInfoList.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfoList);
        //保存房间配套关系
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        List<RoomFacility> roomFacilityList = new ArrayList<>();
        for (Long facilityInfoId : facilityInfoIds) {
            RoomFacility roomFacility = RoomFacility.builder()
                    .roomId(roomSubmitVo.getId())
                    .facilityId(facilityInfoId)
                    .build();
            roomFacilityList.add(roomFacility);
        }
        roomFacilityService.saveBatch(roomFacilityList);
        //保存房间标签关系
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        List<RoomLabel> roomLabelList = new ArrayList<>();
        for (Long labelInfoId : labelInfoIds) {
            RoomLabel roomLabel = RoomLabel.builder()
                    .roomId(roomSubmitVo.getId())
                    .labelId(labelInfoId)
                    .build();
            roomLabelList.add(roomLabel);
        }
        roomLabelService.saveBatch(roomLabelList);
        //保存房间支付方式关系
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
        for (Long paymentTypeId : paymentTypeIds) {
            RoomPaymentType roomPaymentType = RoomPaymentType.builder()
                    .roomId(roomSubmitVo.getId())
                    .paymentTypeId(paymentTypeId)
                    .build();
            roomPaymentTypeList.add(roomPaymentType);
        }
        roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        //保存房间租期关系
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
        for (Long leaseTermId : leaseTermIds) {
            RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                    .roomId(roomSubmitVo.getId())
                    .leaseTermId(leaseTermId)
                    .build();
            roomLeaseTermList.add(roomLeaseTerm);
        }
        roomLeaseTermService.saveBatch(roomLeaseTermList);
        //保存房间属性值关系
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
        for (Long attrValueId : attrValueIds) {
            RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                    .roomId(roomSubmitVo.getId())
                    .attrValueId(attrValueId)
                    .build();
            roomAttrValueList.add(roomAttrValue);
        }
        roomAttrValueService.saveBatch(roomAttrValueList);
    }

    /**
     * @author gengjian
     * @date 2024/12/11 17:01:31
     * 根据条件分页查询房间列表 及房间对应公寓信息
     */
    @Override
    public void pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.selectPageRoomItem(page,queryVo);
    }

    /**
     * @author gengjian
     * @date 2024/12/12 12:14:00
     * 根据房间id获取房间详情（房间信息/公寓信息/房间配套/房间属性/房间标签/支付方式/租期/图片）
     */
    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        //根据房间id获取房间信息
        RoomInfo roomInfo = super.getById(id);
        //根据房间id获取所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //根据房间id获取房间图片信息 公寓管理定义过，item_id既表示公寓也是房间
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVoList(id,ItemType.ROOM);
        //根据房间id通过关系表获取房间配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectRoomFacilityInfoList(id);
        //根据房间id通过关系表获取房间标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectRoomLabelInfoList(id);
        //根据房间id通过关系表获取房间属性vo
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueVoList(id);
        //根据房间id通过关系表获取支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectPaymentTypeList(id);
        //根据房间id通过关系表获取租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermList(id);
        //设置到封装好的RoomDetailVo中
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //copy房间信息
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        return roomDetailVo;
    }
}




